package my.project.sniff2;

import java.util.ArrayList;



import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.View;

public class DrawCanvasView extends View{
	
	private ArrayList<Picture> track=new ArrayList<Picture>(); //for storing a set of pictures,
	//which looks like track
	
	int screenPosition=0; //position of screen(horizontal or vertical)
	int horizontalSize=500;
	int verticalSize=720;
	int trackWidth=30;
	
	public DrawCanvasView(Context context) {
		super(context);
		setFocusable(true); 
		createTrack(context);
	}
	
	//create track with coordinates depends on position of screen
	public void createTrack(Context context) {
		if (screenPosition==0) {
			int x=0, y=verticalSize/2-trackWidth;
			for (int i=0; i<1000; i++) {
				x+=5;
				if (x>horizontalSize) {
					x=0;
					y+=10;
				}
				Picture pict=new Picture(x,y,BitmapFactory.decodeResource(context.getResources(), R.drawable.element));
				track.add(pict);
			}
		}
	}
	
	@Override
    public boolean onTouchEvent(MotionEvent event) {
        int xCoord =(int) event.getX();
        int yCoord =(int) event.getY();
        
        for (int i=0; i<track.size(); i++) {
        	if ( (xCoord<track.get(i).getX() && yCoord<track.get(i).getY())
        			&& (xCoord+32>track.get(i).getX() && yCoord+32>track.get(i).getY()))
        		track.remove(i);
        }
        invalidate();
        
        return true;
    }
	
	@Override
	protected void onDraw(Canvas canvas) {
		for (Picture p: track) {
			canvas.drawBitmap(p.getBitmap(), p.getX(),
					p.getY(),p.getPaint());
		}
	}
	

}
